package com.kc.sjwh.service;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.OrganDao;
import com.kc.sjwh.entity.po.Organ;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;

/**
 * 单位service
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年8月4日
 */
@Service
public class OrganService {
	static Logger logger = Logger.getLogger(OrganService.class);
	@Autowired
	OrganDao organDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<Organ> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("dwdm");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("(dwdm like ? or dwjc like ?)".replace("?", "'%"+v+"%'"));
		}
		
		return organDao.getPage("get", q);
	}

	/**
	 * 新增
	 * @param one
	 */
	public void add(Organ one, String userName) {
		one.setCjr(userName);
		one.setCjsj(new Date());
		organDao.add(one);
	}

	/**
	 * 修改
	 * @param one
	 */
	@Transactional
	public void update(Organ one, String userName) {
		one.setGxr(userName);
		one.setGxsj(new Date());
		organDao.update(one, "dwdm");
	}

	/**
	 * 删除
	 * @param id
	 */
	public void delete(String id) {
		int n = organDao.delete(id);
		if(n == 0) {
			throw new IllegalArgumentException("要删除的记录不存在");
		}
	}

	/**
	 * 打开表单
	 * @param id
	 * @return
	 */
	public Organ form(String id) {
		if(id==null) {
			return null;
		}
		Organ one = organDao.getOne(new QueryParam().put("dwdm", id));
		return one;
	}

	
}
